package com.noahplat.person.service.impl;

import com.noahplat.common.utils.IdWorker;
import com.noahplat.common.utils.Result;
import com.noahplat.common.utils.StatusCode;
import com.noahplat.person.mapper.PersonMapper;
import com.noahplat.person.pojo.Person;
import com.noahplat.person.service.PersonService;
import io.seata.spring.annotation.GlobalTransactional;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;
import tk.mybatis.mapper.entity.Example;

import java.util.*;

/**
 * @ClassName: PersonServiceImpl
 * @Description: 人员service实现类
 * @Author 李景星
 * @Date 2021/3/5
 * @Version 1.0
 */
@Service
public class PersonServiceImpl implements PersonService {

    @Autowired
    private PersonMapper personMapper;

    /**
     * 初始化人员
     * @param userId
     * @return
     */
    @Override
    public Result initPerson(Long userId) {
        IdWorker idWorker = new IdWorker();
        //new一个person,用于初始化person表
        Person person = new Person();
        person.setAcctid((long) 1);//默认注册账号初始化账套id都为1
        person.setUserid(userId);
        person.setCreatedtime(new Date());
        person.setDel("N");
        person.setNo(String.valueOf(idWorker.nextId()));
        //登记数据库
        int res = personMapper.insert(person);
        if(res == 0){//登记数据库失败，调用方需要手动回滚
            return new Result(false, StatusCode.ERROR, "初始化人员信息失败");
        }
        return new Result(true, StatusCode.OK, "初始化人员信息成功", person.getId());
    }

    /**
     * 宋彦磊
     * 查询uid
     */
    public Result<List<Person>> getUid(Long[] pid){
        List<Long> list = new ArrayList<>(Arrays.asList(pid));
        Example example = new Example(Person.class);
        example.createCriteria().andIn("id",list);
        List<Person> list1 = personMapper.selectByExample(example);
        if(ObjectUtils.isEmpty(list1)){
            return new Result<List<Person>>(false,StatusCode.NOTFOUNDERROR,"查无数据");
        }

        return new Result<List<Person>>(true,StatusCode.OK,"查询成功",list1);
    }

    /**
     * 查询人员数量
     * @param acctid
     * @return
     */
    @Override
    public Result getPersonCount(Long acctid){
        Example example = new Example(Person.class);
        example.createCriteria().andEqualTo("acctid",acctid).andEqualTo("del","N");
        int count = personMapper.selectCountByExample(example);
        return new Result(true,StatusCode.OK,"查询成功",count);
    }

    /**
     * 更新人员
     * @param person
     * @return
     */
    @Override
    public Result updatePerson(Person person) {
        int res = personMapper.updateByPrimaryKeySelective(person);
        if(res > 0){
            return new Result(true, StatusCode.OK, "更新成功");
        }
        return new Result(false, StatusCode.ERROR, "更新失败");
    }

    /**
     * 根据账户ID更新人员所属账套
     * @param acctid
     * @param uid
     * @return
     */
    @Override
    public Result updateAcctidByUid(Long acctid, Long uid) {
        int res = personMapper.updateAcctidByUid(acctid, uid);
        if(res > 0){
            return new Result(true, StatusCode.OK, "更新成功");
        }
        return new Result(false, StatusCode.ERROR, "更新失败");
    }
}
